package web

import (
	"bbs-web/internal/web/handler"
	"bbs-web/internal/web/jwtx"
	"bbs-web/internal/web/vo"
	"bbs-web/pkg/ginplus"
	"github.com/gin-gonic/gin"
)

type Router struct {
	userHdl *handler.UserHandler
	codeHdl *handler.CaptchaHandler
	artHdl  *handler.ArticleHandler
}

func NewRouter(userHandler *handler.UserHandler, captchaHandler *handler.CaptchaHandler, artHdl *handler.ArticleHandler) *Router {
	return &Router{
		userHdl: userHandler,
		codeHdl: captchaHandler,
		artHdl:  artHdl,
	}
}

func (r *Router) RegisterURL(engine *gin.Engine) {
	engine.POST("/sign-up", ginplus.WrapJson[vo.RegisterUserReq](r.userHdl.Register))
	engine.GET("/code", ginplus.Wrap(r.codeHdl.ImageCaptcha))
	engine.POST("/pwd-login", ginplus.WrapJson[vo.PwdLoginReq](r.userHdl.PwdLogin))

	userGroup := engine.Group("/users")
	{
		userGroup.POST("/user-create", ginplus.WrapBodyAndToken[vo.CreateUserReq, jwtx.UserClaims](r.userHdl.CreateUser))
		//userGroup.POST("/user-list", ginplus.WrapJWTJson[vo.UserSearchReq](r.userHdl.GetUserList))
	}

	articleGroup := engine.Group("/articles")
	{
		articleGroup.POST("/list", ginplus.WrapBodyAndToken[vo.ArticleListReq, jwtx.UserClaims](r.artHdl.List))   // 创作者查看自己的文章列表
		articleGroup.POST("/edit", ginplus.WrapBodyAndToken[vo.ArticleReq, jwtx.UserClaims](r.artHdl.Edit))       // 新建编辑文章
		articleGroup.POST("/publish", ginplus.WrapBodyAndToken[vo.ArticleReq, jwtx.UserClaims](r.artHdl.Publish)) // 发布某一篇文章
		articleGroup.POST("/:id/withdraw", ginplus.WrapToken[jwtx.UserClaims](r.artHdl.Withdraw))                 // 下架某一篇文章
		articleGroup.GET("/detail/:id", ginplus.WrapToken[jwtx.UserClaims](r.artHdl.Detail))
	}

	pubGroup := engine.Group("/pub")
	{
		pubGroup.GET("/list/:id", ginplus.Wrap(r.artHdl.PubAuthorArtList))                                        //  用户查看某个作者的所有文章,列表资源太大，需要协程异步加载
		pubGroup.GET("/:id", ginplus.WrapToken[jwtx.UserClaims](r.artHdl.PubDetail))                              // 读取文章详情
		pubGroup.GET("/like", ginplus.WrapBodyAndToken[vo.LikeReq, jwtx.UserClaims](r.artHdl.Liked))              // 取消和点赞某个文章
		pubGroup.GET("/collection", ginplus.WrapBodyAndToken[vo.CollectReq, jwtx.UserClaims](r.artHdl.Collected)) // 取消和收藏某个文章
	}

	collectionGroup := engine.Group("/collection")
	{
		collectionGroup.GET("/:id/list", ginplus.Wrap(r.artHdl.GetCollectById)) // 通过用户id获取对应收藏夹列表
		collectionGroup.GET("/create", ginplus.WrapBodyAndToken[vo.CreateCollectReq, jwtx.UserClaims](r.artHdl.CreateCollect))
		//collectionGroup.GET("/:cid/entity/:bid", ginplus.WrapToken[jwtx.UserClaims](r.artHdl.CollectEntityByID))
	}
}
